package com.fzdy.dataplatform.filter;


import com.fzdy.dataplatform.webutil.AllModule;
import com.fzdy.dataplatform.webutil.WebUtilify;
import org.apache.commons.lang.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class PrivilegeFilter implements Filter {

    @Autowired
    private WebUtilify webUtilify;
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest) servletRequest;
        HttpServletResponse httpResponse = (HttpServletResponse) servletResponse;
        String servletPath = httpRequest.getRequestURI().toString();
        System.out.println(servletPath);
        if (!"/sysmanage/device/export".equals(servletPath))
        if (!"/sysmanage/account/LoginError".equals(servletPath) && !"/sysmanage/account/userLogin".equals(servletPath)) {
            if (webUtilify.isAdmin(httpRequest) == null) {
                httpResponse.sendRedirect("/sysmanage/account/LoginError");
                return;
            }
            if (!"/sysmanage/account/loginOut".equals(servletPath)) {
                if (!webUtilify.isAdmin(httpRequest)) {
                    int[] ringtIds = webUtilify.LoginUserRight(httpRequest);
                    Integer rightid = AllModule.allModule.get(servletPath);
                    if (rightid==null||!ArrayUtils.contains(ringtIds,rightid)) {
                        httpResponse.sendRedirect("/sysmanage/account/LoginError");
                        return;
                    }
                }
            }
        }
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {
    }

}
